package com.ccy.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccy.common.utils.PageUtils;
import com.ccy.common.utils.Query;
import com.ccy.ware.dao.PurchaseDetailDao;
import com.ccy.ware.entity.PurchaseDetailEntity;
import com.ccy.ware.service.PurchaseDetailService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String key = (String) params.get("key");
        String wareId = (String) params.get("wareId");
        String status = (String) params.get("status");
        String sidx = (String) params.get("sidx");
        String order = (String) params.get("order");
        String orderRule = String.format("order by %s %s", sidx, order);

        LambdaQueryWrapper<PurchaseDetailEntity> queryWrapper = Wrappers.<PurchaseDetailEntity>lambdaQuery()
                .eq(StringUtils.isNotBlank(wareId), PurchaseDetailEntity::getWareId, wareId)
                .eq(StringUtils.isNotBlank(status), PurchaseDetailEntity::getStatus, status)
                .and(StringUtils.isNotBlank(key), wrapper -> wrapper.like(PurchaseDetailEntity::getPurchaseId, key).or().like(PurchaseDetailEntity::getSkuId, key))
                .last(StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order), orderRule);
        IPage<PurchaseDetailEntity> page = this.page(new Query<PurchaseDetailEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

}